package hu.tozo.facade;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.input.PropertyFileInputHandler;
import org.apache.tools.ant.listener.MailLogger;

/**
 * Ennek az osztalynak a fealdata, hogy letrehozzon egy Ant Task futtato
 * kornyezetet.
 * @author tozo
 */
public class GlassfishFacade {
    
    static String separator = System.getProperty("file.separator");
    static String homeDir = System.getProperty("user.home");
    private String nodeName; //Itt tarolom az inditani kivant nodenak an evet.
    private String ipAddress; //Itt tarolom a DAS node ip cimet amit be kell majd allitani ha frissites tortenik.
    private String kimenet; //ant task kimenete
    
    public GlassfishFacade(){
            nodeName="";
            nodeName="";
    }
    
    public List<String> listTasks(){ 
        return null;
    }
    public void runTask(String taskName) throws UnsupportedEncodingException{
        
        Project project = new Project();
        project.init();
        
        //Ezekre az attributumokra lehet hivatkozni az ant build.xml-ben
        project.setProperty("nodeName", nodeName);
        project.setProperty("ipAddress",ipAddress);
        
       // DefaultLogger logger = new DefaultLogger();
        //Ennek a feladata, hogy azokat a visszateresi ertekeket elkapja
        //amivel utana dolgozni szeretnenk.
        DefaultLogger myLogger = new DefaultLogger();
        
        if(taskName.equals("CPULoad")){
            myLogger.setMessageOutputLevel(Project.MSG_ERR);
            myLogger.setOutputPrintStream(System.out);            
        }
        else{
            myLogger.setMessageOutputLevel(Project.MSG_INFO);
            myLogger.setOutputPrintStream(System.out);

        }
        project.addBuildListener(myLogger);
        File buildFile = new File(homeDir+separator+"aws","build.xml");
        ProjectHelper.configureProject( project, buildFile);
        System.out.print("Anyad!");
        if(taskName.equals("CPULoad")){
            //Elmenti a régi output streamet
            PrintStream old = System.out;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            System.setOut(ps);
            project.executeTarget(taskName);
            System.out.flush();
            System.setOut(old);  
            setKimenet(baos.toString());
        }
        else{
            project.executeTarget(taskName);
            
        }
        //Feladat lefuttatasa, de mar az uj setreamre.
        // Show what happened
        //System.out.println("Here: " + baos.toString());     
        // project.executeTarget(taskName);
        // project.executeTarget("start-domain");        
        
    } 
    /*public void startClusterNode(){
        
    }
    public void stopClusterNode(){
        
    }
    public List<String> listClusterNodes(){
        return null;
    }*/

    /**
     * @return the nodeName
     */
    public String getNodeName() {
        return nodeName;
    }

    /**
     * @param nodeName the nodeName to set
     */
    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    /**
     * @return the ipAddress
     */
    public String getIpAddress() {
        return ipAddress;
    }

    /**
     * @param ipAddress the ipAddress to set
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    /**
     * @return the kimenet
     */
    public String getKimenet() {
        return kimenet;
    }

    /**
     * @param kimenet the kimenet to set
     */
    public void setKimenet(String kimenet) {
        this.kimenet = kimenet;
    }
}
